package component.global.manager
{
	import component.controls.interfaces.ITip;
	import component.controls.interfaces.ITipDisplayObject;
	import component.controls.ui.Tip;
	import component.global.utils.DisplayUtils;
	
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;

	public class TipsMgr
	{
		public static const ONE_LINE_TIP:String = "component.controls.ui.Tip";
		public function TipsMgr()
		{
			_tipDisplays = new Dictionary(true);
			_tips = new Dictionary(true);
		}
		
		private static var _instance:TipsMgr
		
		public static function get Instance():TipsMgr{
			if(_instance == null){
				_instance = new TipsMgr();
			}
			return _instance;
		}
		
		private var _tipDisplays:Dictionary;
		private var _tips:Dictionary;
		
		public function setup():void{
			Tip;
		}
		
		public function addTip(tipDisplayObject:ITipDisplayObject):ITipDisplayObject{
			if(_tipDisplays[tipDisplayObject] != null)return tipDisplayObject;
			_tipDisplays[tipDisplayObject] = tipDisplayObject;
			EventMgr.Instance.addEvent(tipDisplayObject.asDisplayObject as InteractiveObject,[MouseEvent.ROLL_OVER,MouseEvent.ROLL_OUT],[show,remove]);
			return tipDisplayObject;
		}
		
		public function removeTip(tipDisplayObject:ITipDisplayObject):ITipDisplayObject{
			EventMgr.Instance.removeEvent(tipDisplayObject.asDisplayObject as InteractiveObject,[MouseEvent.ROLL_OVER,MouseEvent.ROLL_OUT],[show,remove]);
			DisplayUtils.removeDisplay(_tips[tipDisplayObject.tipType]);
			if(_tips[tipDisplayObject.tipType])_tips[tipDisplayObject.tipType].stopDrag();
			delete _tipDisplays[tipDisplayObject];
			return tipDisplayObject;
		}
		
		private function show(e:Event):void{
			var tipDisplayObject:ITipDisplayObject = e.target as ITipDisplayObject;
			var tip:ITip;
			if(tipDisplayObject.tipType == null)return;
			if(_tips[tipDisplayObject.tipType] == null){
				_tips[tipDisplayObject.tipType] = createTipRef(tipDisplayObject.tipType);
			}
			tip = _tips[tipDisplayObject.tipType] as ITip;
			if(tipDisplayObject.tipData == null)return;
			tip.tipData = tipDisplayObject.tipData;
			tip.asDisplayObject.x = StageManager.Instance.stage.mouseX + 10;
			tip.asDisplayObject.y = StageManager.Instance.stage.mouseY - tip.asDisplayObject.height - 10;
			EventMgr.Instance.addEvent(tipDisplayObject.asDisplayObject as InteractiveObject,[MouseEvent.MOUSE_MOVE],[__onMove]);
			DisplayUtils.addDisplayObject(StageManager.Instance.stage,tip);
		}
		
		private function remove(e:Event):void{
			var tipDisplayObject:ITipDisplayObject = e.target as ITipDisplayObject;
			if(_tips[tipDisplayObject.tipType] == null)return;
			_tips[tipDisplayObject.tipType].stopDrag();
			DisplayUtils.removeDisplay(_tips[tipDisplayObject.tipType]);
			EventMgr.Instance.removeEvent(tipDisplayObject.asDisplayObject as InteractiveObject,[MouseEvent.MOUSE_MOVE],[__onMove]);
		}
		
		private function createTipRef(def:String):*{
			var classRef:* = getDefinitionByName(def) as Class;
			var tipRef:Object = new classRef();
			return tipRef;
		}
		
		private function __onMove(e:Event):void{
			_tips[_tipDisplays[e.currentTarget].tipType].startDrag();
		}
	}
}